<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\bill;

use app\common\dao\BaseDao;
use app\common\model\bill\BillCateModel;
use app\traits\CategoryDao;

class BillCateDao extends BaseDao
{

    use CategoryDao;

    /**
     * @return BillCateModel
     */
    protected function getModel(): string
    {
        return BillCateModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
                $query->where('pid', (int)$where['pid']);
            })
            ->when(isset($where['is_class']) && $where['is_class'] !== '', function ($query) use ($where) {
                if (is_array($where['is_class'])) {
                    $query->whereIn('is_class', $where['is_class']);
                } else {
                    $query->where('is_class', (int)$where['is_class']);
                }
            })
            ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use ($where) {
                $query->where('is_show', (int)$where['is_show']);
            })
            ->when(isset($where['set_finance']) && $where['set_finance'] !== '', function ($query) use ($where) {
                $query->where('set_finance', (int)$where['set_finance']);
            })
            ->when(isset($where['level']) && $where['level'] !== '', function ($query) use ($where) {
                $query->where('level', (int)$where['level']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('name|remark', '%' . trim($where['keywords']) . '%');
            });
    }
}
